package cc.vv.baselibrary.util

import android.app.DatePickerDialog
import android.content.Context
import java.util.*

/**
 * Date 2018/7/16
 * Describe  系统原生日历选择控件
 *
 * @auther liuxin
 */
object DatePickerUtils {

    /**
     * 系统日期选择控件
     */
    private var datePicker: DatePickerDialog? = null
    /**
     * 日期选择监听
     */
    private var chooserListener: DateChooserListener? = null

    /**
     * 日期选择监听
     */
    private val datePickerListener: DatePickerDialog.OnDateSetListener = DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
        chooserListener!!.onDatePicker(year, month + 1, dayOfMonth)
    }

    /**
     * 显示系统日期选择弹窗
     */
    fun showDatePickerDialog(context: Context, chooserListener: DateChooserListener) {
        this.chooserListener = chooserListener
        //获取当前日期
        val calendar = Calendar.getInstance()
        val mYear: Int = calendar.get(Calendar.YEAR)
        val mMonth = calendar.get(Calendar.MONTH)
        val mDay = calendar.get(Calendar.DAY_OF_MONTH)

        datePicker = DatePickerDialog(context, datePickerListener, mYear, mMonth, mDay)

        if (!datePicker!!.isShowing) datePicker!!.show() else datePicker!!.hide()
    }

    interface DateChooserListener {
        /**
         * 日期选择回调
         *
         * @param year  年
         * @param month 月   范围: 0~11
         * @param dayOfMonth 日
         */
        fun onDatePicker(year: Int, month: Int, dayOfMonth: Int)

    }
}